/** @license opt.js
 * Copyright (c) 2009 Fedor Indutny
 * License: GNU General Public License version 3 or later
 */
var template='<tr id=tr_%4><td class=%3><input id=RegEx%4 value=%1></td><td class=%3><input id=Down%4 value=%2></td><td><div id=dbutt_%4><input type=button class=plus_button_%3 id=butt_%4 onclick="operateLines(\'%4\',\'%3\')"></div></td><tr>';
var template2='<td class=%3><input id=RegEx%4 value=%1></td><td class=%3><input id=Down%4 value=%2></td><td><div id=dbutt_%4><input type=button class=plus_button_%3 id=butt_%4 onclick="operateLines(\'%4\',\'%3\')"></div></td>';
var buttcnt=0;
function saveTrackers() {
  var x=0;
  for (var i=0;i<buttcnt;i++)
    if (document.getElementById('tr_'+i)) {
      localStorage[x]=document.getElementById('RegEx'+i).value;
      localStorage['_'+x]=document.getElementById('Down'+i).value;
      x++;
    }
      localStorage[x]='';
      localStorage['_'+x]='';
  var status = document.getElementById("savebutton");
  status.value = "...saved...";
  setTimeout(function() {
    status.value='SAVE';
  }, 750);
  chrome.extension.connect().postMessage('#^%rebuild');
  loadTrackers();
}                                      
function showRegExp() {
  window.open('http://en.wikipedia.org/wiki/Regular_expression');
}
function loadTrackers() {
  buttcnt=0;
  var t=  document.getElementById('trackers')
  t.innerHTML= "<table>";
  t.innerHTML+="<tr class=Trackers><th>Site's <a class=rx onclick='showRegExp();'>RegExp</a>:\
<BR><font class=ex>f.e. <u>http:\\/\\/torrents\\.ru/.*</u></font></th><th>Download <a class=rx onclick='showRegExp();'>RegExp</a>:<br>\
<font class=ex>\\/forum\\/dl\\.php\\?t=.*</font></th><th> </th></tr>";
  for (var i=0;(localStorage[i]!='')&&(localStorage[i]!=null);i++) {
      t.innerHTML+=template.replace(/%1/mgi,localStorage[i]).replace(/%2/mgi,localStorage['_'+i]).replace(/%3/mgi,'minus').replace(/%4/mgi,i)
    buttcnt++;
  }
  t.innerHTML+=template.replace(/%1/mgi,'').replace(/%2/mgi,'').replace(/%3/mgi,'plus').replace(/%4/mgi,i)
  buttcnt++;
  t.innerHTML+='</table>';
  t=null;                     
}
function addTracker(i) {
  var t=  document.getElementById('trackers')
  var ntr=document.createElement("tr");
  ntr.id='tr_'+i;
  ntr.innerHTML=template2.replace(/%1/mgi,'').replace(/%2/mgi,'').replace(/%3/mgi,'plus').replace(/%4/mgi,i);
  t.appendChild(ntr);
}
function deleteTracker(i) {
  document.getElementById('trackers').removeChild(document.getElementById('tr_'+i));
}
function operateLines(p,a) {
  if (a=='plus') {
    document.getElementById('dbutt_'+p).innerHTML= document.getElementById('dbutt_'+p).innerHTML.replace(/_plus/gim,'_minus').replace(/'plus/gim,'\'minus');
    document.getElementById('tr_'+p).innerHTML=document.getElementById('tr_'+p).innerHTML.replace(/="plus"/gim,'="minus"');
    p.onclick='operateLines(minus)';
    addTracker(buttcnt);                               
    buttcnt++;
  } else {
    deleteTracker(p);
  }
}
function refreshTheme() {
  var Theme128=[];
  Theme128.push('torrent-128x128.png');
  Theme128.push('u-128x128.png');
  Theme128.push('vuze-128x128.png'); 
  Theme128.push('trans-128x128.png');              
  document.getElementById('theme128').src=Theme128[localStorage['theme']];
  if (localStorage['theme']!=2) document.getElementById('theme128').style.left=0
  else  document.getElementById('theme128').style.left=10;
  document.getElementById('theme128').style.visibility='visible';
}
function checkInfo(v) {
  if (v) 
    localStorage['download-name']=''
  else
    localStorage['download-name']='don\'t';
}                  
function checkC(v1) {
  if (v1) 
    localStorage['ck']=1
  else
    localStorage['ck']=0;
}                  
function checkA(v1) {
  if (v1) 
    localStorage['ak']=1
  else
    localStorage['ak']=0;
}                  
function checkS(v1) {
  if (v1) 
    localStorage['sk']=1
  else
    localStorage['sk']=0;
}                  
function checkM(v1) {
  if (v1) 
    localStorage['mouse']=1
  else
    localStorage['mouse']=0;
}                  

function loadInfo() {
  if (localStorage['download-name']!='don\'t')
    document.getElementById('download-name').checked=true;
  if (localStorage['ck']!='0')
    document.getElementById('ck').checked=true;
  if (localStorage['ak']!='0')
    document.getElementById('ak').checked=true;
  if (localStorage['sk']!='0')
    document.getElementById('sk').checked=true;
  if (localStorage['mouse']!='0')
    document.getElementById('mouse').checked=true;

  loadTrackers();
  document.getElementById('theme').value=localStorage['theme'];
  refreshTheme();
  document.getElementById('maindiv').style.visibility='visible';
}